global myvarlist equity purchaseprice deposit_final loan_final defprice
global regvar age_borrower i.advtype	gross_income  i.employment
global regvarnotype age_borrower gross_income  i.employment

*--------------------------------------------------------------------------------

use "${datadir}\HTB_main_cleaned_feb2016", replace

*--- Get regional house prices (as control)
drop index
merge m:1 regionname datem using ///
	"${datadir}\local_price_appreciation", ///
	keepusing(index) gen(merge_region) keep(1 3)

gen london = region == 5


*--- Deflated house prices
gen rescale_index = .
forvalues i = 1/9 {
	sum index if datem == ym(2015, 8) & region == `i'
	replace rescale_index = index / r(mean) * 100 if region == `i'
}
gen defprice = purchaseprice / rescale_index * 100


*--- Run diff-in-diff with South-East	
*--- Match floor area
merge 1:1 psd_id using "${datadir}\HTB_main_feb2016_sqft", nogen 

foreach x in $myvarlist total_floor_area {
	eststo `x': reg `x' london##post rescale_index ${regvar}, cluster(postcode3)
	estadd local borrower "Yes"
	estadd local hp "Yes"
}
esttab ${myvarlist} total_floor_area ///  
	using "${tabledir}\feb2016_did_SE_rescale.tex"  ///
	, se b(2) keep(1.london 1.post 1.london#1.post) ///
	order(1.london#1.post 1.london 1.post) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	coeflabel(1.london#1.post		"\textsc{\textbf{London $\times$ Post Jan 2016}}" ///
			1.london				"\textsc{London}" ///
			1.post 					"\textsc{Post Jan 2016}") ///
	stats(borrower hp r2 N, fmt (0 0 2 %7.0fc) ///
	labels( `"\textsc{Borrower characteristics}"' `"\textsc{Regional house price index}"' ) ) ///
	mtitles("\textsc{\shortstack{Equity\\loan}}" "\textsc{\shortstack{Purchase\\price}}" ///
		"\textsc{\shortstack{Down\\payment}}" "\textsc{\shortstack{Mortgage\\amount}}"  /// 
		"\textsc{\shortstack{Deflated\\purchase price}}" /// 
		"\textsc{\shortstack{Square \\meters}}" ) replace nogap nonotes 
	

*--- Parallel trend Charts
local vars $myvarlist total_floor_area
local ylabs `" "Equity loan" "Purchase price" "Down payment" "Mortgage amount" "Deflated purchase price" "Square meters" "'
local n: word count `vars'
forvalues i = 1/`n' {
	local var: word `i' of `vars'
	local ylab: word `i' of `ylabs'
	
	if "`var'" == "deposit_final" {
		local ytick "ylab(25(10)55)"
	}
	else {
		local ytick
	}
	preserve
	collapse `var', by(datem london)

	*--- Chart
	format %tm datem
	line `var' datem if london == 0, lwidth(medthick) || ///
		line `var' datem if london == 1, lwidth(medthick) ///
		lpattern(dash) `ytick' ///
		${graphoption} xtitle("") xlabel(667(3)678)  ///
		ytitle("`ylab'") ///
		leg(order(1 "South-East" 2 "London")) ///
		xline(673, lpattern(dash) lcolor(black)) ///
		name(`var', replace)
	restore
}
gr combine $myvarlist total_floor_area, r(4) ysize(8) scale(0.8) ///
	graphregion(color(white))
graph export "${projectdir}\Figure\South-East_London_ptrend.pdf", ///
		as(pdf) replace
	

	
	
*--- Run diff-in-diff with South-East (placebo)

use "${datadir}\HTB_main_cleaned_feb2015_placebo", replace

*--- Get regional house prices (as control)
drop index
merge m:1 regionname datem using ///
	"${datadir}\local_price_appreciation", ///
	keepusing(index) gen(merge_region) keep(1 3)

gen london = region == 5



*--- Deflated house prices
gen rescale_index = .
forvalues i = 1/9 {
	sum index if datem == ym(2014, 8) & region == `i'
	replace rescale_index = index / r(mean) * 100 if region == `i'
}
gen defprice = purchaseprice / rescale_index * 100


keep if region == 5 | region == 6

*--- Match floor area
merge 1:1 psd_id using "${datadir}\HTB_main_cleaned_feb2015_placebo_sqft", nogen

eststo clear
foreach x in $myvarlist total_floor_area {
	eststo `x': reg `x' london##post rescale_index ${regvar}, cluster(postcode3)
	estadd local borrower "Yes"
	estadd local hp "Yes"
}
esttab ${myvarlist} total_floor_area ///
	///using "${tabledir}\feb2015_did_ctrl2_SE_rescale_placebo.tex" ///
	, se b(2) ///
	keep(1.london 1.post 1.london#1.post) ///
	order(1.london#1.post 1.london 1.post) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	coeflabel(1.london#1.post		"\textsc{\textbf{London $\times$ Post Jan 2015}}" ///
			1.london				"\textsc{London}" ///
			1.post 					"\textsc{Post Jan 2015}") ///
	stats(borrower hp r2 N, fmt (0 0 2 %7.0fc) ///
	labels( `"\textsc{Borrower characteristics}"' `"\textsc{Regional house price index}"' ) ) ///
	mtitles("\textsc{\shortstack{Equity\\loan}}" "\textsc{\shortstack{Purchase\\price}}" ///
		"\textsc{\shortstack{Down\\payment}}" "\textsc{\shortstack{Mortgage\\amount}}" /// 	
		"\textsc{\shortstack{Deflated\\purchase price}}" ///
		"\textsc{\shortstack{Square \\meters}}") replace ///
	mgroups("Values in \pounds1,000", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span ) /// 
	nogap nonotes 
